var editor;
window.onload = function () {
    viewBulletin();
}

layui.use(['layer', 'upload'], function () {
    var $ = layui.jquery;
    var layer = layui.layer,
        upload = layui.upload;
    //普通图片上传开始
    var uploadInst = upload.render({
        elem: '#id_upload_img',
        url: '/FIleUploadController.do?action=upload',
        size: 1024, //限制文件大小，单位 KB
        before: function (obj) {
            //预读本地文件示例，不支持ie8
            obj.preview(function (index, file, result) {
                $('#id_upload_img').attr('style', 'width: auto;height: auto;max-width: 20%;max-height: 20%; border-radius: 15px;');
                $('#id_upload_img').attr('src', result); //图片链接（base64）
            });
        },
        done: function (res) {
            //如果上传失败
            if (res.code != 200) {
                return layer.msg('上传失败');
            }
            //上传成功
            var path = res.data;
            $('#bulletinImgPath').val(path);
        },
        error: function () {
            //演示失败状态，并实现重传
            var demoText = $('#demoText');
            demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
            demoText.find('.demo-reload').on('click', function () {
                uploadInst.upload();
            });
        }
    });
});

/**
 * 頁面加載的時候获取公告信息
 */
function viewBulletin() {
    var param = {
        bulletinId: $('#bulletinId').val()
    };
    $.ajax({
        type: "POST",
        url: "/BulletinController.do?action=getBulletinById&bulletinId="+$('#bulletinId').val(),
        //contentType: "application/json",
        sync: true,
        //data: param,
        dataType: "json",
        success: function (result) {
            //alert(JSON.stringify(result));
            if (result.code == 200) {
                var bulletin = result.data;
                $('#id_upload_img').attr('style', 'width: auto;height: auto;max-width: 20%;max-height: 20%; border-radius: 15px;');
                $('#id_upload_img').attr('src', bulletin.bulletinImg); //图片链接（base64）
                $("#bulletinTitle").val(bulletin.bulletinTitle);
                $("#bulletinImgPath").val(bulletin.bulletinImg);
                $("#bulletinContent").empty().append(bulletin.bulletinContent);
                const E = window.wangEditor;
                editor = new E('#bulletinContent');
                // 设置编辑器高度
                editor.config.height = 480;
                editor.create()
            } else {
                $("#bulletinContent").empty().append('<p>该公告不存在！<p/>');
                console.log("该公告不存在！")
            }
        }
    });
}

function modifyBulletin() {
    // 获取表单参数
    var bulletinId = $("#bulletinId").val(); // 标题
    var bulletinTitle = $("#bulletinTitle").val(); // 标题
    var bulletinImgPath = $("#bulletinImgPath").val(); // 配图

    const E = window.wangEditor;
    //var bulletinContent = editor.txt.text(); // 内容
    var bulletinContent = editor.txt.html(); // 内容

    var datas="bulletinTitle="+bulletinTitle+"&bulletinImg="+bulletinImgPath+"&bulletinContent="+bulletinContent+"&bulletinId="+bulletinId;

    var param = {
        bulletinId: bulletinId,
        bulletinTitle: bulletinTitle,
        bulletinImg: bulletinImgPath,
        bulletinContent: bulletinContent
    };
    // 检验表单参数
    var verifyResult = verify(param);
    if (verifyResult) {
        // 校验成功，执行添加请求
        $.ajax({
            type: "POST",
            url: "/BulletinController.do?action=update&"+datas,
            contentType: "application/json",
            sync: true,
            dataType: "json",
            success: function (result) {
                if (result.code == 200) {
                    layer.msg('更新公告成功！', {icon: 1, offset: '100px', time: 2000});
                    setTimeout(() => {
                        window.location.href = "/BulletinController.do?action=list";
                    }, 2000);
                } else {
                    layer.msg(result.msg, {icon: 2, offset: '100px', time: 2000});
                }
            }
        });
    }
}

function verify(param) {
    var bulletinTitle = param.bulletinTitle;
    var bulletinContent = param.bulletinContent;

    if ($.isEmptyObject(bulletinTitle)) {
        layer.msg('请填写公告标题！', {icon: 2, offset: '100px', time: 2000});
        return false;
    } else if ($.isEmptyObject(bulletinContent)) {
        layer.msg('请填写公告内容！', {icon: 2, offset: '100px', time: 2000});
        return false;
    }
    return true;
}